// 二分法 时间复杂度 O(log n) 空间复杂度 O(1)

class Solution {
public:
    int search(vector<int>& nums, int target) {
        int n = nums.size();
        if (n == 1) return nums[0] == target ? 0 : -1;
        int left = 0, right = n - 1, mid;
        while (left <= right) {
            mid = left + (right - left) / 2;
            if (nums[mid] < nums[0]) {
                right = mid - 1;
            } 
            else {
                left = mid + 1;
            }
        }
        if (target >= nums[0]) {
            right = left - 1;
            left = 0;
        }
        else {
            right = n - 1;
        }
        while (left <= right) {
            mid = left + (right - left) / 2;
            if (nums[mid] > target) {
                right = mid - 1;
            }
            else if (nums[mid] < target) {
                left = mid + 1;
            }
            else {
                return mid;
            }
        }
        return -1;
    }
};